package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.AdvertManag;
import com.lrkj.property.domain.Community;
import com.lrkj.property.mapper.AdvertManagMapper;
import com.lrkj.property.service.IAdvertManagService;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 广告管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-16
 */
@Service
public class AdvertManagServiceImpl implements IAdvertManagService {
    @Autowired
    private AdvertManagMapper advertManagMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ICommunityService communityService;


    /**
     * 查询广告管理
     *
     * @param id 广告管理ID
     * @return 广告管理
     */
    @Override
    public AdvertManag selectAdvertManagById(String id) {
        return advertManagMapper.selectAdvertManagById(id);
    }

    /**
     * 查询广告管理列表
     *
     * @param advertManag 广告管理
     * @return 广告管理
     */
    @Override
    public List<AdvertManag> selectAdvertManagList(AdvertManag advertManag) {
        return advertManagMapper.selectAdvertManagList(advertManag);
    }

    /**
     * 新增广告管理
     *
     * @param advertManag 广告管理
     * @return 结果
     */
    @Override
    public int insertAdvertManag(AdvertManag advertManag) {
        advertManag.setId(IdUtils.simpleUUID());
        advertManag.setCreateBy(SecurityUtils.getUsername());
        advertManag.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(advertManag);
        return advertManagMapper.insertAdvertManag(advertManag);
    }

    /**
     * 修改广告管理
     *
     * @param advertManag 广告管理
     * @return 结果
     */
    @Override
    public int updateAdvertManag(AdvertManag advertManag) {
        advertManag.setUpdateTime(DateUtils.getNowDate());
        advertManag.setUpdateBy(SecurityUtils.getUsername());
        this.setUnitInfo(advertManag);
        return advertManagMapper.updateAdvertManag(advertManag);
    }


    private void setUnitInfo(AdvertManag bean) {
        Community parentBean = communityService.selectCommunityById(bean.getCommunityId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getCommunityId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(), bean.getAdvertName(), bean.getCommunityId(), OrgConstants.advert, parentBean.getAncestors(), bean.getOrderNum());
    }


    /**
     * 批量删除广告管理
     *
     * @param ids 需要删除的广告管理ID
     * @return 结果
     */
    @Override
    public int deleteAdvertManagByIds(String[] ids) {
        orgTreeService.deleteOrgTreeByIds(ids);
        return advertManagMapper.deleteAdvertManagByIds(ids);
    }

    /**
     * 删除广告管理信息
     *
     * @param id 广告管理ID
     * @return 结果
     */
    @Override
    public int deleteAdvertManagById(String id) {
        return advertManagMapper.deleteAdvertManagById(id);
    }

    @Override
    public Map<String, Object> getAdverPostInfo(String adverId) {
        return advertManagMapper.getAdverPostInfo(adverId);
    }

    /**
     * 批量新增广告管理信息
     *
     * @param advertManags 广告管理对象
     */
    public int batchAddData(List<AdvertManag> advertManags) {
        return advertManagMapper.batchAddData(advertManags);
    }
}
